package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.dto.VehicleQueryDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.ParkingSpot;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Vehicle;
import com.cdu.guoxinan.www.smartcom.domian.vo.VehicleVO;
import com.cdu.guoxinan.www.smartcom.mapper.VehicleMapper;
import com.cdu.guoxinan.www.smartcom.service.VehicleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Resource
    private VehicleMapper vehicleMapper;
    @Override
    public PageInfo<VehicleVO> queryPage(VehicleQueryDTO vehicleQueryDTO) {
        PageHelper.startPage(vehicleQueryDTO.getPage(),vehicleQueryDTO.getLimit());
        List<VehicleVO> list = vehicleMapper.queryPage(vehicleQueryDTO);
        PageInfo<VehicleVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean add(Vehicle vehicle) {
        int result = vehicleMapper.add(vehicle);
        return result > 0;
    }

    @Override
    public boolean update(Vehicle vehicle) {
        int result = vehicleMapper.update(vehicle);
        return result > 0;
    }

    @Override
    public boolean delete(Integer id) {
        int result = vehicleMapper.delete(id);
        return result > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean parkingSpot(Vehicle vehicle) {
        int result1 = vehicleMapper.updateParkingSpotId(vehicle);
        ParkingSpot parkingSpot = new ParkingSpot();
        parkingSpot.setParkingSpotId(vehicle.getParkingSpotId());
        int result2 = vehicleMapper.updateParkingSpotStatus(parkingSpot);
        return result1 > 0 && result2 > 0;
    }
}
